<?php

namespace App\Admin\Controllers\Teaching;

use App\Models\Student;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use App\Models\Squad;
use http\Env\Response;
use Illuminate\Http\Request;
use Encore\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;


use App\Models\Class_section_group;


class analysisController extends Controller
{

    public function __construct()
    {
        // $arr = DB::table("kly_notice")->getArr(['id','starttime']);
        // foreach($arr as $v){
        // 	DB::table("kly_notice")->where('id',$v['id'])->update([
        // 		'starttime' => date("Y-m-d H:i:s",strtotime($v['starttime']) - 2592000)
        // 	]);
        // }
        // exit("done");
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
        $content->header('报表详情');
        $content->description('详情');
        $squad = Squad::all()->pluck('name','id');
        $content->row(view('admin.teaching.analysis',compact('squad')));
        return $content;
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {

    }


    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {

    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $type = request('type');
        return Admin::grid(kly_report::class,function(Grid $grid) use ($type) {
            if($type == 'day'){
                $type = '1';
            }elseif($type == 'week'){
                $type = '2';
            }elseif($type == 'full'){
                $type = '3';
            }

            $id = Admin::user()->id;
            $camps = DB::table('kly_camps')->where('tutors',$id)->orWhereRaw('find_in_set('.$id.',`aids`)')->get();
            $small_camps = array(0);
            foreach($camps as $v){
                $small_camps[] = $v->id;
            }
            $grid->model()->where('pid',0)->where('type',$type)->whereIn('small_camp',$small_camps);
            $grid->orderBy('id','desc');

            $grid->name('名称');
            $grid->column('大营')->display(function(){
                $pid = Kly_camp::find($this->small_camp)->pid;
                return Kly_camp::find($pid)->name;
            });
            $grid->column('小营')->display(function(){
                return Kly_camp::find($this->small_camp)->name;
            });
            $grid->type('类型')->display(function(){
                if($this->type == '1'){
                    return '日报';
                }elseif($this->type == '2'){
                    return '周报';
                }elseif($this->type == '3'){
                    return '整报';
                }else{
                    return '';
                }
            });
            $grid->start_time('开始时间');
            $grid->end_time('结束时间');

            $grid->column('操作')->display(function(){
                $html = '<a class="btn btn-success" href="/admin/kly_report_list?pid='.$this->id.'">个人报表</a>';
                return $html;
            });

            $grid->disableActions();
            $grid->disableFilter();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disableExport();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Squad::class,function (Form $form) {
            $form->setTitle('查看');
            $form->row(function ($row) use ($form)
            {
                $id = Admin::user()->id;
                $row->width(4)->select('section', trans('admin.section'))->options(function(){
                    return Class_section_group::regulate_options(2);
                })->attribute(['id'=>'section'])->Myload('group', '/api/get_group_user',$id)->rules('required');
                $row->width(4)->select('group',trans('admin.group'))->options(function ($id){
                    return Class_section_group::options($id);
                })->attribute(['id'=>'group'])->Myload('squad','/api/get_squads_user',$id)->rules('required');
                $row->width(4)->select('squad',trans('admin.squad'))->attribute(['id'=>'squad'])->load('student','/api/get_students')->rules('required');
            },  $form);

            $form->row(function ($row) use ($form)
            {
                $row->width(4)->select('student','学生id-姓名')->attribute(['id'=>'student'])->rules('required');
                //$row->width(4)->text('start_time','开始时间(包含)')->attribute(['id'=>'startTime']);
                //$row->width(4)->text('end_time','结束时间(包含)')->attribute(['id'=>'endTime']);
                $row->width(4)->date('start_time','开始时间(包含)')->attribute(['id'=>'startTime','style'=>'width:350px;']);
                $row->width(4)->date('end_time','结束时间(包含)')->attribute(['id'=>'endTime','style'=>'width:350px;']);
            },  $form);

            $form->row(function ($row) use ($form)
            {
                $status = array();
                $status = array('1'=>'听写','2'=>'测验','3'=>'考勤','4'=>'家校','5'=>'作业');
                $row->width(4)->multipleSelect('type','类型')->options($status)
                    ->attribute(['id'=>'type']);
                $row->width(4)->html('<textarea placeholder="请填写评语,填写时使用回车换行，方便排版。" id="memo" style="height:150px;resize:none;width:396px;margin-left:12px;"></textarea>
<span class="nl_zsjc"><i class="nlysrzs">0</i>/<em class="nlzzts">160</em> 个字</span>');
                $row->width(4)->html('<button style="margin-left:170px;" type="button" id="btn_submit" class="btn btn-primary">查询</button><button style="" type="button" id="btn_pdf" class="btn btn-danger" onclick="o_window()">导出PDF</button>');
            },  $form);

            $form->disableSubmit();
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList();
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }

    public function submit(Request $request)
    {
        $data = array();
        $data = DB::table('exam_analysis')->where('squad_id',$request['id'])->get();

        $jindu = $age = '';

        foreach($data as $v){
            $jindu = $v->study_jindu;
            $age = $v->age;
        }

        $group = Squad::find($request['id'])->group;
        $squads = Squad::pluck('name','id')->toArray();

        $group_data = DB::table('exam_analysis')->where('study_jindu',$jindu)->where('age',(string)$age)->get();



        $num1 = $num2 = array(0=>0,1=>0,2=>0,3=>0,4=>0,5=>0);
        $num3 = $num4 =  array(0=>0,1=>0,2=>0,3=>0,4=>0);
        $num5 = array(0=>0,1=>0,2=>0,3=>0);$max = 0;$min=9999;$test_score = 0;
        $avg_num = $avg_total = 0;
        $knum = array(0=>0,1=>0,2=>0,3=>0);
        $jx_total = $kq_total = 0;


        $line1 = $line2 = array();
        foreach($group_data as $v){
            if($v->squad_id == $request['id']){
                $avg_num++;
                $avg_total+=$v->kq_rate;
                if($v->kq_rate > 0){
                    if($v->kq_rate < 0.90){
                        $num1[0]++;
                    }elseif($v->kq_rate >= 0.90 && $v->kq_rate < 0.92){
                        $num1[1]++;
                    }elseif($v->kq_rate >= 0.92 && $v->kq_rate < 0.94){
                        $num1[2]++;
                    }elseif($v->kq_rate >= 0.94 && $v->kq_rate < 0.96){
                        $num1[3]++;
                    }elseif($v->kq_rate >= 0.96 && $v->kq_rate < 0.98){
                        $num1[4]++;
                    }elseif($v->kq_rate >= 0.98 && $v->kq_rate <= 1){
                        $num1[5]++;
                    }

                    if($v->kq_rate < 0.90){
                        $knum[0]++;
                    }elseif($v->kq_rate >= 0.9 && $v->kq_rate < 0.95){
                        $knum[1]++;
                    }elseif($v->kq_rate >= 0.95 && $v->kq_rate < 0.975){
                        $knum[2]++;
                    }elseif($v->kq_rate >= 0.975 && $v->kq_rate <= 1){
                        $knum[3]++;
                    }
                }
                if($v->jx_rate > 0){
                    if($v->jx_rate < 0.90){
                        $num2[0]++;
                    }elseif($v->jx_rate >= 0.90 && $v->jx_rate < 0.92){
                        $num2[1]++;
                    }elseif($v->jx_rate >= 0.92 && $v->jx_rate < 0.94){
                        $num2[2]++;
                    }elseif($v->jx_rate >= 0.94 && $v->jx_rate < 0.96){
                        $num2[3]++;
                    }elseif($v->jx_rate >= 0.96 && $v->jx_rate < 0.98){
                        $num2[4]++;
                    }elseif($v->jx_rate >= 0.98 && $v->jx_rate <= 1){
                        $num2[5]++;
                    }
                }
                if($v->total_score > 0){
                    $test_score += $v->total_score;
                    if($v->total_score < $min){
                        $min = $v->total_score;
                    }
                    if($v->total_score > $max){
                        $max = $v->total_score;
                    }
                    if($v->total_score < 60){
                        $num5[0]++;
                    }elseif($v->total_score >= 60 && $v->total_score < 80){
                        $num5[1]++;
                    }elseif($v->total_score >= 80 && $v->total_score < 90){
                        $num5[2]++;
                    }elseif($v->total_score >= 90 && $v->total_score <= 100){
                        $num5[3]++;
                    }
                }

                if($v->total_score > 0 && $v->kq_rate > 0){
                    $line1[] = $v->total_score;
                    $line2[] = $v->kq_rate;
                }
            }

            if($v->kq_rate > 0){
                $kq_total += $v->kq_rate;
                if($v->kq_rate < 0.80){
                    $num3[0]++;
                }elseif($v->kq_rate >= 0.80 && $v->kq_rate < 0.85){
                    $num3[1]++;
                }elseif($v->kq_rate >= 0.85 && $v->kq_rate < 0.90){
                    $num3[2]++;
                }elseif($v->kq_rate >= 0.90 && $v->kq_rate < 0.95){
                    $num3[3]++;
                }elseif($v->kq_rate >= 0.95 && $v->kq_rate <= 1){
                    $num3[4]++;
                }
            }
            if($v->jx_rate > 0){
                $jx_total += $v->jx_rate;
                if($v->jx_rate < 0.80){
                    $num4[0]++;
                }elseif($v->jx_rate >= 0.80 && $v->jx_rate < 0.85){
                    $num4[1]++;
                }elseif($v->jx_rate >= 0.85 && $v->jx_rate < 0.90){
                    $num4[2]++;
                }elseif($v->jx_rate >= 0.90 && $v->jx_rate < 0.95){
                    $num4[3]++;
                }elseif($v->jx_rate >= 0.95 && $v->jx_rate <= 1){
                    $num4[4]++;
                }
            }
        }


        $jige_temp = $youxiu_temp = array();
        $jige = $youxiu = array();


        $title = array();
        $total = 0;
        $num = array();
        foreach($group_data as $v){
            if(is_null($v->total_score) || $v->total_score == 0 || $v->total_score == ''){
                continue;
            }
            if(!isset($jige_temp[$v->squad_id])){
                $jige_temp[$v->squad_id] = 0;
            }
            if(!isset($youxiu_temp[$v->squad_id])){
                $youxiu_temp[$v->squad_id] = 0;
            }
            if(!isset($num[$v->squad_id])){
                $num[$v->squad_id] = 0;
            }
            if($v->total_score >= 60){
                $jige_temp[$v->squad_id] += 1;
            }
            if($v->total_score >= 80){
                $youxiu_temp[$v->squad_id] += 1;
            }
            $num[$v->squad_id] += 1;
        }

        foreach($jige_temp as $k => $v){
            $title[] = $squads[$k];
            $jige[] = ($num[$k] == 0) ? 0 : round(($v / $num[$k] * 100),2);
        }
        foreach($youxiu_temp as $k => $v){
            $youxiu[] = ($num[$k] == 0) ? 0 : round(($v / $num[$k] * 100),2);
        }

        //听力 语言体系 词汇 阅读 书写的能力柱状图
        //考勤出勤率分布（班级、学段）
        //家校准交率分布（班级、学段）
        //测试成绩分布趋势
        //当前学段下所有班级及格率及优秀率

        $score1 = $total1 = 0;
        $score2 = $total2 = 0;
        $score3 = $total3 = 0;
        $score4 = $total4 = 0;
        $score5 = $total5 = 0;

        $score6 = $total6 = 0;
        $score7 = $total7 = 0;
        $score8 = $total8 = 0;
        $score9 = $total9 = 0;
        $score10 = $total10 = 0;
        $score11 = $total11 = 0;
        $score12 = $total12 = 0;
        $score13 = $total13 = 0;
        $score14 = $total14 = 0;
        $score15 = $total15 = 0;
        $score16 = $total16 = 0;
        $score17 = $total17 = 0;
        $score18 = $total18 = 0;
        $score19 = $total19 = 0;
        $score20 = $total20 = 0;
        $score21 = $total21 = 0;
        $score22 = $total22 = 0;

        $ability = array();
        foreach($data as $v){
            if(isset($v->score1) && $v->score1 >= 0 && !is_null($v->score1)){
                $score1+= $v->score1 * 100 / $v->total1;
                $total1 += 1;
            }
            if(isset($v->score2) && $v->score2 >= 0 && !is_null($v->score2)){
                $score2+= $v->score2 * 100 / $v->total2;
                $total2 += 1;
            }
            if(isset($v->score3) && $v->score3 >= 0 && !is_null($v->score3)){
                $temp_score = $temp_total = 0;

                $temp_score += $v->score3;
                $temp_total += $v->total3;

                if(isset($v->score4) && $v->score4 >= 0 && !is_null($v->score4)){
                    $temp_score += $v->score4;
                    $temp_total += $v->total4;
                }

                $score3 += $temp_score * 100 / $temp_total;

                $total3 += 1;
            }
            if(isset($v->score5) && $v->score5 >= 0 && !is_null($v->score5)){
                $temp_score = $temp_total = 0;

                $temp_score += $v->score5;
                $temp_total += $v->total5;

                if(isset($v->score6) && $v->score6 >= 0 && !is_null($v->score6)){
                    $temp_score += $v->score6;
                    $temp_total += $v->total6;
                }
                if(isset($v->score7) && $v->score7 >= 0 && !is_null($v->score7)){
                    $temp_score += $v->score7;
                    $temp_total += $v->total7;
                }

                $score5 += $temp_score * 100 / $temp_total;

                $total5 += 1;
            }


            if(isset($v->score8) && $v->score8 >= 0 && !is_null($v->score8)){
                $temp_score = $temp_total = 0;

                $temp_score += $v->score8;
                $temp_total += $v->total8;

                if(isset($v->score9) && $v->score9 >= 0 && !is_null($v->score9)){
                    $temp_score += $v->score9;
                    $temp_total += $v->total9;
                }

                $score8 += $temp_score * 100 / $temp_total;

                $total8 += 1;
            }
            if(isset($v->score10) && $v->score10 >= 0 && !is_null($v->score10)){
                $temp_score = $temp_total = 0;

                $temp_score += $v->score10;
                $temp_total += $v->total10;

                if(isset($v->score11) && $v->score11 >= 0 && !is_null($v->score11)){
                    $temp_score += $v->score11;
                    $temp_total += $v->total11;
                }

                $score10 += $temp_score * 100 / $temp_total;

                $total10 += 1;
            }
            if(isset($v->score12) && $v->score12 >= 0 && !is_null($v->score12)){
                $score12+= $v->score12 * 100 / $v->total12;
                $total12 += 1;
            }
            if(isset($v->score13) && $v->score13 >= 0 && !is_null($v->score13)){
                $score13+= $v->score13 * 100 / $v->total13;
                $total13 += 1;
            }
            if(isset($v->score14) && $v->score14 >= 0 && !is_null($v->score14)){
                $score14+= $v->score14 * 100 / $v->total14;
                $total14 += 1;
            }
            if(isset($v->score15) && $v->score15 >= 0 && !is_null($v->score15)){
                $score15+= $v->score15 * 100 / $v->total15;
                $total15 += 1;
            }
            if(isset($v->score16) && $v->score16 >= 0 && !is_null($v->score16)){
                $score16+= $v->score16 * 100 / $v->total16;
                $total16 += 1;
            }
            if(isset($v->score17) && $v->score17 >= 0 && !is_null($v->score17)){
                $score17+= $v->score17 * 100 / $v->total17;
                $total17 += 1;
            }
            if(isset($v->score18) && $v->score18 >= 0 && !is_null($v->score18)){
                $score18+= $v->score18 * 100 / $v->total18;
                $total18 += 1;
            }
            if(isset($v->score19) && $v->score19 >= 0 && !is_null($v->score19)){
                $score19+= $v->score19 * 100 / $v->total19;
                $total19 += 1;
            }
            if(isset($v->score20) && $v->score20 >= 0 && !is_null($v->score20)){
                $score20+= $v->score20 * 100 / $v->total20;
                $total20 += 1;
            }
            if(isset($v->score21) && $v->score21 >= 0 && !is_null($v->score21)){
                $score21+= $v->score21 * 100 / $v->total21;
                $total21 += 1;
            }
            if(isset($v->score22) && $v->score22 >= 0 && !is_null($v->score22)){
                $score22+= $v->score22 * 100 / $v->total22;
                $total22 += 1;
            }
        }

        if($score1 >= 0 && $total1 > 0){
            $ability['title'][] = '听力';
            $ability['data'][] = round($score1 / $total1,2);
        }
        if($score2 >= 0 && $total2 > 0){
            $ability['title'][] = '语法';
            $ability['data'][] = round($score2 / $total2,2);
        }
        if($score3 >= 0 && $total3 > 0){
            $ability['title'][] = '词汇·词意理解';
            $ability['data'][] = round($score3 / $total3,2);
        }
        if($score5 >= 0 && $total5 > 0){
            $ability['title'][] = '句型·句意理解';
            $ability['data'][] = round($score5 / $total5,2);
        }



        if($score8 >= 0 && $total8 > 0){
            $ability['title'][] = '词汇·单词拼写';
            $ability['data'][] = round($score8 / $total8,2);
        }
        if($score10 >= 0 && $total10 > 0){
            $ability['title'][] = '句型·情景对话';
            $ability['data'][] = round($score10 / $total10,2);
        }
        if($score12 >= 0 && $total12 > 0){
            $ability['title'][] = '句型·补全对话';
            $ability['data'][] = round($score12 / $total12,2);
        }
        if($score13 >= 0 && $total13 > 0){
            $ability['title'][] = '句型·句子书写';
            $ability['data'][] = round($score13 / $total13,2);
        }
        if($score14 >= 0 && $total14 > 0){
            $ability['title'][] = '阅读·信息定位';
            $ability['data'][] =round( $score14 / $total14,2);
        }
        if($score15 >= 0 && $total15 > 0){
            $ability['title'][] = '阅读·完形填空+阅读理解';
            $ability['data'][] = round($score15 / $total15,2);
        }
        if($score16 >= 0 && $total16 > 0){
            $ability['title'][] = '词汇·情景对话';
            $ability['data'][] = round($score16 / $total16,2);
        }
        if($score17 >= 0 && $total17 > 0){
            $ability['title'][] = '句型·改错';
            $ability['data'][] = round($score17 / $total17,2);
        }
        if($score18 >= 0 && $total18 > 0){
            $ability['title'][] = '阅读·阅读理解';
            $ability['data'][] = round($score18 / $total18,2);
        }
        if($score19 >= 0 && $total19 > 0){
            $ability['title'][] = '语法·句型转化';
            $ability['data'][] =round( $score19 / $total19,2);
        }
        if($score20 >= 0 && $total20 > 0){
            $ability['title'][] = '句子书写';
            $ability['data'][] = round($score20 / $total20,2);
        }
        if($score21 >= 0 && $total21 > 0){
            $ability['title'][] = '阅读·完形填空';
            $ability['data'][] = round($score21 / $total21,2);
        }
        if($score22>= 0 && $total22 > 0){
            $ability['title'][] = '语法·单词拼写';
            $ability['data'][] = round($score22 / $total22,2);
        }


        $tscore = $tnum = array();
        $test =  DB::table('exam_analysis')->where('squad_id',$request['id'])
            ->groupBy(['total_score'])->orderBy('total_score','ASC')
            ->select(DB::raw('count(*) as num'),'total_score')
            ->get();
        foreach($test as $v){
            $tscore[] = $v->total_score;
            $tnum[] = $v->num;
        }
        $test_avg=array_sum($num5) ? round($test_score/array_sum($num5),2) : 0;
        $kq_avg=array_sum($num3) ? round($kq_total/array_sum($num3)*100,2) : 0;
        $jx_avg=array_sum($num4) ? round($jx_total/array_sum($num4)*100,2) : 0;
        return array('jige'=>$jige,'youxiu'=>$youxiu,'title'=>$title,'ability'=>$ability,'tscore'=>$tscore,'tnum'=>$tnum,'knum'=>$knum,'banji_kaoqin'=>$num1,'banji_jiaxiao'=>$num2,'test'=>$num5,'min'=>$min,'max'=>$max,'test_avg'=>$test_avg,'kq_avg'=>$kq_avg,'jx_avg'=>$jx_avg,'line1'=>$line1,'line2'=>$line2);
    }
}
